package com;

/**
 * @author 吴宇~
 * @motto 我亦无他，唯手熟尔。
 * @edition jdk1.8
 * @reflection 0-1 喧闹任其喧闹，自由我自为之，我自风情万种，与世无争。
 * @person Han Xiaojiu
 */
public class Shot implements Runnable {
    int x;
    int y;
    int direct = 0;
    int speed = 5;
    boolean isLive = true;

    public Shot(int x, int y, int direct) {
        this.x = x;
        this.y = y;
        this.direct = direct;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(50);//线程休眠50毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //根据方向改变坐标
            switch (direct) {
                case 0:
                    y -= speed;
                    break;
                case 1:
                    x += speed;
                    break;
                case 2:
                    y += speed;
                    break;
                case 3:
                    x -= speed;
                    break;
            }
            //测试，输出xy坐标
//            System.out.println("子弹 x=" + x + " y=" + y);
            if (!(x >= 0 && x <= 1000 && y >= 0 && y <= 750&&isLive)) {
                //子弹移动到面板边界自动销毁
                System.out.println("子弹线程退出");
                isLive=false;
                break;
            }
        }
    }
}
